﻿using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Diagnostics;

using Builderator.Core.Plugin;
using Builderator.Core.Interfaces;

namespace Builderator.Core.Action
{
    
    public class SubversionPlugin : PluginBase, ISource
    {
        [DocumentProperty("Executable", typeof(Uri), false, DocumentProperty.BrowseUriType.File, "Executable|*.exe")]
        public string Executable
        {
            get;
            set;
        }

        [DocumentProperty("URL", typeof(String), false)]
        public string URL
        {
            get;
            set;
        }

        [DocumentProperty("Output", typeof(Uri), false, DocumentProperty.BrowseUriType.Folder, "")]
        public string Output
        {
            get;
            set;
        }

        public SubversionPlugin()
            : base("Subversion", new Guid("50AB1966-E3C8-11E0-9DDC-FFB64824019B"))
        {

        }

        public int GetRevision()
        {
            StringBuilder builder = new StringBuilder();

            Process process = new Process();

            builder.AppendFormat("info {0} --xml", this.URL);

            process.StartInfo.Arguments = builder.ToString();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.FileName = this.Executable;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;

            process.Start();

            string xml = string.Empty;
            StreamReader output = process.StandardOutput;

            while (!output.EndOfStream)
            {
                xml = output.ReadToEnd();
            }

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);

            XmlElement rootElement = xmlDoc.DocumentElement;

            return int.Parse(rootElement.ChildNodes[0].Attributes["revision"].Value);
        }

        public override ExecutionResult Run()
        {
            StringBuilder builder = new StringBuilder();

            Process process = new Process();

            builder.Append("co");

            builder.AppendFormat(" {0} {1}", this.URL, this.Output);

            this.DebugMessage(builder.ToString());

            process.StartInfo.Arguments = builder.ToString();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.FileName = this.Executable;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.EnableRaisingEvents = true;

            process.Exited += new EventHandler(process_Exited);

            process.Start();

            this.DebugMessage(process.StandardOutput);

            return base.Run();
        }

        private void process_Exited(object sender, EventArgs e)
        {
            this.Running = false;

            this.ExitCode = ((Process)sender).ExitCode;
        }
    }
}